package com.ruoyi.project.experiment.experimentstudent.controller;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.project.experiment.experimentstudent.domian.EchartResult;
import com.ruoyi.project.experiment.experimentstudent.domian.StatusOther;
import com.ruoyi.project.experiment.experimentstudent.domian.StudentExperiment;
import com.ruoyi.project.experiment.experimentstudent.service.ITbExperimentReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName EchartController
 * @Description TODO
 * @Author lvyongwei
 * @Date
 **/
@Controller
@CrossOrigin
@RequestMapping("/system/echarts")
public class EchartController {

    @Autowired
    private ITbExperimentReportService iTbExperimentReportService;

    /*@Autowired
    private RedisUtil redisUtil;*/

    /**
     * 获取每个实验以及其体验人次
     * @return
     */
    @GetMapping("/stuCount")
    @ResponseBody
    public List<StatusOther> getStuCount(){
        List<StatusOther> list = iTbExperimentReportService.getStuCountOther();
        return list;
    }


    /**
     * 获取指定实验分数分布情况
     * @return
     */
    @GetMapping("/experimentScore")
    @ResponseBody
    public EchartResult experimentScore(Long experimentId){
       return iTbExperimentReportService.getScore(experimentId);
    }

    /**
     * 获取指定实验分数分布情况
     * @return
     */
    @GetMapping("/getDescribe")
    @ResponseBody
    public JSONObject getDescribe(){
        List<EchartResult> list = iTbExperimentReportService.getDescribe();
        /**
         * 定义传出对象
         */
        JSONObject jsonObject = new JSONObject();
        /**
         * 定义最高分集合
         */
        List<Double> maxScore = new ArrayList<>();
        List<Double> minScore = new ArrayList<>();
        List<Double> maxTime = new ArrayList<>();
        List<Double> minTime = new ArrayList<>();
        List<String> experimentName = new ArrayList<>();
        for(int i=0;i<list.size();i++){
            maxScore.add(list.get(i).getMaxScore());
            minScore.add(list.get(i).getMinScore());
            maxTime.add(Double.valueOf(Math.round(list.get(i).getMaxTime()/60/1000)));
            minTime.add(Double.valueOf(Math.round(list.get(i).getMinTime()/60/1000)));
            experimentName.add(list.get(i).getExperimentName());
        }
        jsonObject.put("maxScore",maxScore);
        jsonObject.put("minScore",minScore);
        jsonObject.put("maxTime",maxTime);
        jsonObject.put("minTime",minTime);
        jsonObject.put("experimentName",experimentName);
        return jsonObject;
    }

    /**
     * 获取指定实验分数分布情况
     * @return
     */
    /*@GetMapping("/getVisitCount")
    @ResponseBody
    public Integer getVisitCount(){
       Integer count = 0;
        *//**
         * 訪問總人數
         *//*
        if(org.springframework.util.StringUtils.isEmpty(redisUtil.get("shdxEasy:visitCount")) || redisUtil.get("shdxEasy:visitCount")==null){
            redisUtil.set("shdxEasy:visitCount",1);
            count = 1;
        }else{
            count = (Integer) redisUtil.get("shdxEasy:visitCount");
            count ++;
            //将访问次数放入redis
            redisUtil.set("shdxEasy:visitCount",count);
        }
        return count;
    }*/
}
